package com.example.studentcoursesysteam.Controller;

import com.example.studentcoursesysteam.Bean.Teacher;
import com.example.studentcoursesysteam.Repository.CourseRepository;
import com.example.studentcoursesysteam.Repository.StudentCourseRepository;
import com.example.studentcoursesysteam.Repository.StudentRepository;
import com.example.studentcoursesysteam.Repository.TeacherRepository;
import com.example.studentcoursesysteam.Vo.TeacherCourseMaxMinAvgVo;
import com.example.studentcoursesysteam.Vo.TeacherCourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    StudentRepository studentRepository;
    @Autowired
    CourseRepository courseRepository;
    @Autowired
    StudentCourseRepository studentCourseRepository;
    @Autowired
    TeacherRepository teacherRepository;
    @RequestMapping("/insertTeacher")
    public String insertTeacher( Teacher teacher){
        if (!teacherRepository.existsByCode(teacher.getCode())){
            teacherRepository.save(teacher);
            return "添加成功";
        }else
            return "职工号已存在，添加失败";
    }

    @RequestMapping("/updateTeacher")
    public String updateTeacher(Teacher teacher) {
        teacherRepository.save(teacher);
        return "修改成功";
    }

    @RequestMapping("/deleteTeacher")
    public String deleteTeacher(String code){
        if (teacherRepository.existsByCode(code)){
            teacherRepository.deleteTeacher(code);
            courseRepository.getDeleteByTeacherCode(code);
            return "删除成功";
        }else
            return "老师不存在，删除失败";

    }

    @RequestMapping("/selectTeacherCourse")
    public List<TeacherCourseVo> selectTeacherCourse(String code){
        return teacherRepository.selectTeacherCourse(code);
    }

    @RequestMapping("/selectTeacher")
    public List<Teacher> selectTeacher(String code,String password){
        return teacherRepository.findByCodeAndPassword(code,password);
    }

    @RequestMapping("/selectTeacherCode")
    public List<Teacher> selectTeacherCode(String code){
        return teacherRepository.findByCode(code);
    }

    @RequestMapping("/selectTeacherCourseAchievement")
    public List<TeacherCourseMaxMinAvgVo> selectTeacherCourseAchievement(String code){
        return teacherRepository.selectTeacherCourseAchievement(code);
    }

    @RequestMapping("/selectTeacherCourseAchievementName")
    public List<TeacherCourseMaxMinAvgVo> selectTeacherCourseAchievementName(String name){
        return teacherRepository.selectTeacherCourseAchievementName(name);
    }
}
